package org.dtrd.modules.evaluation.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.common.aspect.annotation.AutoLog;
import org.dtrd.modules.evaluation.constant.Evaluation;
import org.dtrd.modules.evaluation.entity.bean.parq.ParqDetail;
import org.dtrd.modules.evaluation.entity.bean.parq.ParqListInfo;
import org.dtrd.modules.evaluation.entity.bean.parq.ParqListRequest;
import org.dtrd.modules.evaluation.entity.bean.parq.ParqPageRequest;
import org.dtrd.modules.evaluation.service.IDtrdEntEvaluationRdParqService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static org.dtrd.common.constant.CommonConstant.*;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_OPERATION_FAILURE;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_PARAMS_MISSING;

/**
 * <p>
 *  前端控制器
 * </p>
 * @author xiao
 * @since 2022-08-15
 */
@Api(tags="量表评估")
@RestController
@RequestMapping("/evaluation/parq/")
public class ParqController extends BaseController {

    @Autowired
    private IDtrdEntEvaluationRdParqService parqService;

    @ApiOperation(value = "获取最近一次PARQ（体适能量表）记录", response = ParqListInfo.class)
    @GetMapping("get")
    @AutoLog(value="获取最近一次PARQ（体适能量表）记录", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getParq(@RequestParam Integer patientId){
        ParqListInfo info = parqService.getCurrentParq(patientId);
        return Result.OK(info);
    }

    @ApiOperation(value = "查询PARQ（体适能量表）评估列表", response = ParqListInfo.class)
    @GetMapping("list")
    @AutoLog(value="查询PARQ（体适能量表）评估列表", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getParqList(@RequestParam("patientId") Integer patientId){
        List<ParqListInfo> list = parqService.getParqList(patientId);
        return Result.OK(list);
    }

//    @ApiOperation(value = "分页查询列表", response = ParqListInfo.class)
//    @PostMapping("")
//    @AutoLog(value="查询列表", operateType = OPERATE_TYPE_QUERY)
//    public Result<?> getParqList(@RequestBody ParqPageRequest request){
//        handlePageRequest(request);
//        IPage<ParqListInfo> page = parqService.pageParq(request);
//        return Result.OK(page);
//    }

    @ApiOperation("新增或修改PARQ（体适能量表）")
    @PostMapping("save")
    @AutoLog(value="新增或修改PARQ（体适能量表）", operateType = OPERATE_TYPE_SAVE_OR_UPDATE)
    public Result<?> saveOrUpdateParq(@RequestBody @Validated ParqDetail dto){
        if(dto.getAnswers().size() != Evaluation.ProblemNum.PARQ.getNum()){
            return error(STATE_PARAMS_MISSING);
        }
        boolean result = parqService.saveOrUpdateParq(dto);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }

    @ApiOperation("删除一条PARQ（体适能量表）记录")
    @DeleteMapping("delete")
    @AutoLog(value="删除一条PARQ（体适能量表）记录", operateType = OPERATE_TYPE_DELETE)
    public Result<?> removeParq(@RequestParam Integer id){
        if(isIntParamInvalid(id)) {
            return error(STATE_PARAMS_MISSING);
        }
        boolean result = parqService.removeParq(id);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }
}
